/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import utils.ReportUtils;
import utils.XmlUtils;

public class ReportController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SAXException {
        response.setContentType("text/html;charset=UTF-8");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            String action = request.getParameter("action");
            String webPath = getServletContext().getRealPath("/");
            String xmlProPath = webPath + "WEB-INF/XmlDoc/Products.xml";
            String xmlOrderDetailPath = webPath + "WEB-INF/XmlDoc/OrderDetails.xml";
            String xmlOrderPath = webPath + "WEB-INF/XmlDoc/Orders.xml";
            String xmlUserPath = webPath + "WEB-INF/XmlDoc/Users.xml";
            String xmlUserDetailPath = webPath + "WEB-INF/XmlDoc/UserDetails.xml";
            String xslBestUser = webPath + "WEB-INF/Report/BestUsers.xsl";
            String xslBestPro = webPath + "WEB-INF/Report/BestProducts.xsl";
            String foUserPath = webPath + "WEB-INF/Report/Users.fo";
            String foProPath = webPath + "WEB-INF/Report/Products.fo";

            byte[] content = new byte[0];
            if (action.equals("ON_PRODUCT")) {
                Map<String, Object> param = new LinkedHashMap<String, Object>();
                param.put("orderdetailpath", xmlOrderDetailPath);
                bout = ReportUtils.createFileFo(webPath, xslBestPro, xmlProPath, param, foProPath);
                content = bout.toByteArray();
            }else if (action.equals("ON_USER")) {
                Map<String, Object> param = new LinkedHashMap<String, Object>();
                param.put("orderdetailpath", xmlOrderDetailPath);
                param.put("orderpath", xmlOrderPath);
                param.put("userdetailpath", xmlUserDetailPath);
                bout = ReportUtils.createFileFo(webPath, xslBestUser, xmlUserPath, param, foUserPath);
                content = bout.toByteArray();
            }
            response.setContentType("application/pdf");
            response.setContentLength(content.length);
            response.getOutputStream().write(content);
            response.getOutputStream().flush();
        } catch(Exception ex){
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }finally  {
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SAXException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SAXException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
